"use strict";
//FYI: https://github.com/Tencent/puerts/blob/master/doc/unity/manual.md
exports.__esModule = true;
var FairyEditor = CS.FairyEditor;
var App = FairyEditor.App;
function get_package_all_depends(pkg) {
    var query = new FairyEditor.DependencyQuery();
    query.QueryDependencies(pkg.items, FairyEditor.DependencyQuery.SeekLevel.ALL);
    var set = [];
    var list = query.resultList;
    for (var index_1 = 0; index_1 < list.Count; index_1++) {
        var item = list.get_Item(index_1);
        var name_1 = item.item.owner.name;
        if (set.indexOf(name_1) === -1)
            set.push(name_1);
    }
    var name = pkg.name;
    var index = set.indexOf(name);
    if (index >= 0)
        set.splice(index, 1);
    return set;
}
function list_all_circle_depends(pkg, dep) {
    var query = new FairyEditor.DependencyQuery();
    query.QueryDependencies(pkg.items, FairyEditor.DependencyQuery.SeekLevel.ALL);
    var list = query.resultList;
    for (var index = 0; index < list.Count; index++) {
        var item = list.get_Item(index);
        if (item.item.owner.name == dep) {
            console.warn(pkg.name + " is depend " + item.item.name + " in " + dep);
        }
    }
}
function check_circle_depends() {
    var packages = App.project.allPackages;
    var map = new Map();
    var keys = [];
    for (var index = 0; index < packages.Count; index++) {
        var pkg = packages.get_Item(index);
        keys.push(pkg.name);
        map.set(pkg.name, get_package_all_depends(pkg));
    }
    for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
        var key = keys_1[_i];
        var value = map.get(key);
        console.log("package :" + key + " depends on {" + value.join(',') + "}");
        for (var _a = 0, value_1 = value; _a < value_1.length; _a++) {
            var dep = value_1[_a];
            if (map.has(dep) && map.get(dep).indexOf(key) !== -1) {
                console.error("package " + key + " is circle depends with " + dep);
                list_all_circle_depends(App.project.GetPackageByName(key), dep);
            }
        }
    }
    console.log('check completed!');
}
var editMenu = App.menu.GetSubMenu("tool");
editMenu.AddItem("检查循环依赖", "check_all_depences", 1, false, check_circle_depends);
function onDestroy() {
    editMenu.RemoveItem("check_all_depences");
}
exports.onDestroy = onDestroy;
